<?php
class StatusFilter extends AbstractFilter {

	protected $_criteria = null;
	
	public $status;
	
	public function init($criteria) {
		$this->_criteria = $criteria;
		if (isset($_GET['status'])) $this->status = $_GET['status'];
	}
	
	public function apply() {
		if ($this->status) {
			$this->_criteria->addAndCondition('status='.$this->status, 'status');
		}
	}

	public function getClearUrl() {
		$gets = $_GET;
		unset($gets['status']);
		return Yii::app()->createAbsoluteUrl('site/index', $gets);
	}
	
	public function isApplied() {
		return isset($_GET['status']);
	}
	
	public function getItems() {
		$c = clone $this->_criteria;
		$c->removeCondition('status');
		$c->group = 'status';
		$c->select = 'status, COUNT(p.id) as count';
		$c->addAndCondition('status is not null');
		$cmd = $c->getCommand();
		$cmd->from = Post::model()->tableName().' AS p';

		$data = $cmd->queryAll();
		$list = Post::model()->getStatusListData();
		$result = array();
		foreach($data as $d) {
			$result[$d['status']] = $list[$d['status']].' <span class="count">('.$d['count'].')</span>';
		}
		return $result;
	}

	public function getTemplate() {
		return '/catalog/filter/status';
	}
	
}